package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.impl.CategoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "分类模块")
@RequestMapping("/admin/category")
public class CantoryController {
    @Autowired
    private CategoryServiceImpl categoryService;
    //新增分类
    @PostMapping
    @ApiOperation("新增分类")
    public Result add(@RequestBody CategoryDTO dto){
        categoryService.add(dto);
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> getbypage(CategoryPageQueryDTO dto){
        PageResult pageresult = categoryService.getbypage(dto);
        return Result.success(pageresult);
    }

    //启用，禁用分类
    @PostMapping("/status/{status}")
    @ApiOperation("启用,禁用分类")
    public Result startorstop(@PathVariable Integer status,Long id){
        categoryService.startorstop(status,id);
        return Result.success();
    }

    //修改分类
    @PutMapping
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO dto){
        categoryService.update(dto);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result getBytype(Integer type){
         List<Category> list = categoryService.getBytype(type);
         return Result.success(list);
    }

    @DeleteMapping
    @ApiOperation("删除分类")
    public Result deleteByid(String id){
        categoryService.deleteByid(id);
        return Result.success();
    }
}
